/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.sonny.ebook;

import java.io.*;
import java.util.*;
import java.util.zip.*;
//import tools.CompressionUtil;
/**
 * 
 * @author Kevin
 */
public class CompressTools {


	public CompressTools() {
		// throw new UnsupportedOperationException("Not yet implemented");
	}
	public  boolean isValid(File zip) {

		ZipFile zipfile = null;
		try {
			zipfile = new ZipFile(zip);
			return true;
		} catch (ZipException e) {
			return false;
		} catch (IOException e) {
			return false;
		} finally {
			try {
				if (zipfile != null) {
					zipfile.close();
					zipfile = null;
				}
			} catch (IOException e) {
			}
		}
	}

	public  int size(String zipFP) {

		int result = 0;

		ZipFile zipfile = null;
		try {
			zipfile = new ZipFile(zipFP);
			result = zipfile.size();
		} catch (ZipException e) {
			return 0;
		} catch (IOException e) {
			return 0;
		} finally {
			try {
				if (zipfile != null) {
					zipfile.close();
					zipfile = null;
				}
			} catch (IOException e) {
				return 0;
			}
		}
		return result;
	}

	public int count(String ZipFP) {

		int i = 0;
		try {
			FileInputStream fis = new FileInputStream(ZipFP);
			ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));

			// @SuppressWarnings("unused")
			ZipEntry entry;
			while ((entry = zis.getNextEntry()) != null) {
				i++;
			}
			zis.close();
		} catch (Exception e) {
		}
		return i;
	}

	/*
	 * public String[] listContents() {
	 * 
	 * 
	 * //String[] list = null; ArrayList<String> list = new ArrayList<String>();
	 * ArrayList<String> contentFPList = new ArrayList<String>();
	 * 
	 * try { FileInputStream fis = new FileInputStream(ZipFP); ZipInputStream
	 * zis = new ZipInputStream( new BufferedInputStream(fis)); ZipEntry entry;
	 * 
	 * int i = 0; while ((entry = zis.getNextEntry()) != null) { list.add
	 * (entry.toString()); } zis.close(); } catch (Exception e) { } String[] arr
	 * = new String[contentFPList.size()]; return (String[])
	 * contentFPList.toArray(arr); }
	 */

	/**
	 * CREATE TEMP FOLDER 
	 */

	public  String createTempFolder (String srcFP) {

		String newFilePath ="";
		try {
			File newFile = File.createTempFile("CLEANER-", "", new File(srcFP));
			newFilePath = newFile.getPath();
			newFile.delete();
			File newFile2 = new File(newFilePath);
			newFile2.mkdir();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return newFilePath;
	}
	
	/**
	 * DELETE ALL FILES AND SUBFOLDERS
	 */

	public  boolean deletedTempFiles(String dstFolderPath) {

		boolean result = false;
		File temp = new File(dstFolderPath);

		if (temp.exists()) {
			File[] files = temp.listFiles();
			for (int i = 0; i < files.length; i++) {
				if (files[i].isDirectory()) {
					deletedTempFiles(files[i].getPath());
				} else {
					files[i].delete();
				}
			}
			result = true;
		}
		return result;
	}

	/**
	 * UNRAR Commandline utility
	 * @param cmd String[] cmd = new String[3];
	 * return results (Failed or Success) 
	 */
	public static  boolean unrar(String unrarFP, String arg, String srcFP, String destFP) {
		
		boolean result = false; 
		
		try {
			// TODO Auto-generated method stub
			String osName = System.getProperty("os.name");
			
			/*
			 if (osName.equals("Windows NT")) {
				cmd[0] = "cmd.exe";cmd[1] = "/C";cmd[2] = args[0];
			} else if (osName.equals("Windows 95")) {
				cmd[0] = "command.com";cmd[1] = "/C";cmd[2] = args[0];
			}
			*/
			
			String[] cmd = new String[4];
			cmd[0] = unrarFP;
			cmd[1] = arg;
			cmd[2] = srcFP;
			cmd[3] = destFP;

			File destFile = new File(destFP);
			destFile.mkdir();
			Runtime rt = Runtime.getRuntime();
			System.out.println("UNRAR CMD::   " + unrarFP + " " + arg + " " + srcFP + " " +destFP );
			Process proc = rt.exec(cmd);

			// any error or output message?
			StreamGobbler errorGobbler = new StreamGobbler(proc.getErrorStream(), "ERROR");
			StreamGobbler outputGobbler = new StreamGobbler(proc.getInputStream(), "OUTPUT");
			// kick them off
			errorGobbler.start();
			outputGobbler.start();

			// any error???
			int exitVal = proc.waitFor();
			
			
			if (exitVal >0){
				System.out.println("ExitValue: " + exitVal);
				result = false;
			}
			else
			{
				result = true;
			}
			//Force to kill the process
			proc.destroy();
			
		} catch (Throwable t) {
			t.printStackTrace();
		}

		return result;
	}
	public static  String[] unzip(String zipFP, String dstFolderPath) {

		String[] result = null;

		try {
			CompressionUtil cu = new CompressionUtil();
			try {
				File file = new File(dstFolderPath);
				if (!file.exists()) {
					file.mkdirs();
				}
				file = null;
			} catch (Exception e) {
			}

			result = cu.unzip(new File(zipFP), new File(dstFolderPath), "EUC-KR");

		} catch (IOException e) {
			e.printStackTrace();
		}
		return result;
	}

	public  String[] uncmpress(String zipFP,String dstFolderPath) {

		ArrayList<String> contentFPList = new ArrayList<String>();

		// CHECK THE DEST FOLDER EXISTANCE AND CREATE ONE

		try {
			File file = new File(dstFolderPath);
			if (!file.exists()) {
				file.mkdirs();
			}
			file = null;
		} catch (Exception e) {
		}

		// UNCOMPRESS TO THE GIVEN FOLDER
		try {
			int BUFFER = 2048;
			BufferedOutputStream dest = null;
			FileInputStream fis = new FileInputStream(zipFP);
			ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));
			ZipEntry entry;

			while ((entry = zis.getNextEntry()) != null) {
				System.out.print(".");
				int count;
				byte data[] = new byte[BUFFER];
				// write the files to the disk
				FileOutputStream fos = new FileOutputStream(dstFolderPath + "\\" + entry.getName());
				dest = new BufferedOutputStream(fos, BUFFER);
				while ((count = zis.read(data, 0, BUFFER)) != -1) {
					dest.write(data, 0, count);
				}
				contentFPList.add(dstFolderPath + "\\" + entry.getName());
				dest.flush();
				dest.close();
			}

			zis.close();
			fis.close();

		} catch (Exception e) {
			System.err.println("ZIP error ");
		}

		String[] arr = new String[contentFPList.size()];
		return (String[]) contentFPList.toArray(arr);
	}



}
